/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2025-06-04 11:41:17
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2025-06-04 16:45:58
 * @FilePath: /vue3-uniapp/src/service/useEnum/index.ts
 */
import { treeToArray } from '@/utils';

import type { EnumItem } from './type';
import {
    validTypes,
    severityTypes,
    satisfyTypes,
    orderSourceTypesSubject,
    orderSourceTypesSource,
    orderStatusTypesSubject,
    orderStatusTypesSource,
    appealTypeCodeTypesSubject,
    appealTypeCodeTypesSource
} from './modules';
import { excuteSubject } from './utils';

export const useEnum = () => {
    const getLabelByValue = <T>(
        treeOrList: EnumItem<T>[],
        value: string | number,
        treeItemId = 'id' as keyof EnumItem<unknown>
    ) => {
        const arr = treeToArray(treeItemId, treeOrList as EnumItem<unknown>[]);
        const item = arr.find((item) => item.value === value);
        if (item) {
            return item.label;
        }
        return '无效value';
    };

    const getItemByValue = <T>(
        treeOrList: EnumItem<T>[],
        value: string | number,
        treeItemId = 'id' as keyof EnumItem<unknown>
    ) => {
        const data = treeToArray(treeItemId, treeOrList as EnumItem<unknown>[]);
        const item = data.find((item) => item.value === value);
        if (item) {
            return item;
        }
        return undefined;
    };

    const unsubscribeList: (() => void)[] = [];
    onBeforeUnmount(() => {
        unsubscribeList.forEach((fn) => {
            fn();
        });
    });

    // 工单来源
    const orderSourceTypes = ref<EnumItem<DictType>[]>([]);
    const orderSourceTypesUpdate = () => {
        orderSourceTypesSubject.next(undefined);
    };

    // 工单状态
    const orderStatusTypes = ref<EnumItem<DictType>[]>([]);
    const orderStatusTypesUpdate = () => {
        orderStatusTypesSubject.next(undefined);
    };

    // 申诉类型
    const appealTypeCodeTypes = ref<EnumItem<DictType>[]>([]);
    const appealTypeCodeTypesUpdate = () => {
        appealTypeCodeTypesSubject.next(undefined);
    };

    return {
        getLabelByValue,
        getItemByValue,
        validTypes: JSON.parse(JSON.stringify(validTypes)),
        severityTypes: JSON.parse(JSON.stringify(severityTypes)),
        satisfyTypes: JSON.parse(JSON.stringify(satisfyTypes)),
        getOrderSourceTypes: () => {
            const res = excuteSubject(
                orderSourceTypesUpdate,
                orderSourceTypesSource,
                orderSourceTypes
            );
            unsubscribeList.push(res.subscription.unsubscribe);
            return res;
        },
        getOrderStatusTypes: () => {
            const res = excuteSubject(
                orderStatusTypesUpdate,
                orderStatusTypesSource,
                orderStatusTypes
            );
            unsubscribeList.push(res.subscription.unsubscribe);
            return res;
        },
        getAppealTypeCodeTypes: () => {
            const res = excuteSubject(
                appealTypeCodeTypesUpdate,
                appealTypeCodeTypesSource,
                appealTypeCodeTypes
            );
            unsubscribeList.push(res.subscription.unsubscribe);
            return res;
        }
    };
};
